﻿@using VistaOnlineR1._1.Helpers
@using VistaOnlineR1._1.Models
@using DevExpress.Web.Mvc.UI
@using DevExpress.Charts;
@using DevExpress.XtraCharts;

<script type="text/javascript">
    // <![CDATA[
    $(window).load(UpdateLabelPositionDisabled);

    function SetLabelPositionEnabled() {
        $("#labelPosition")[0].disabled = false;
    }
    function UpdateLabelPositionDisabled() {
        $("#labelPosition")[0].disabled = !$("#showLabels")[0].checked;
    }
    // ]]> 
</script>

@using (Html.BeginForm()) {
    GraficoTortaAnilloViewsOptions options = (GraficoTortaAnilloViewsOptions)ViewData[GragicoHelper.OptionsKey];
    <div class="chartOptionsPanel" style="height:80px; width:700px">
        <div style="padding-bottom: 4px;">
            <label style="margin-right:31px" for="view">Formato:</label>
            @Html.DropDownList("view", GragicoHelper.GetPieViews(), new { style = "width:102px" })
            @Html.CheckBox("valueAsPercent", options.ValueAsPercent, new { style = "margin-left: 8px; vertical-align: -2px;" })
            <label class="checkBox" for="valueAsPercent">Valor en Porcentaje</label>
        </div>
        <div style="padding-bottom: 4px;">
            <label style="margin-right:12px" for="labelPosition">Posición Etiqueta:</label>
            @Html.DropDownList("labelPosition", GragicoHelper.GetPieLabelPositions(), new { style = "width:102px" })
            @Html.CheckBox("showLabels", options.ShowLabels, new { style = "margin-left: 8px; vertical-align: -2px;", onclick = "UpdateLabelPositionDisabled()" })
            <label class="checkBox" for="showLabels">Ocultar Etiquetas</label>
        </div>
        <div>
            <div style="float: left; vertical-align:baseline;">
                <label for="explodedPoints">Puntos Explotación:</label>
                @Html.DropDownList("explodedPoints", GragicoHelper.GetPieExplodePoints(), new { style = "width:102px" })
            </div>
            <input style="float: right;" type="submit" value="Aplicar" onclick="SetLabelPositionEnabled()"/>
        </div>
    </div>
    <br/>
    
    ChartControlSettings settings = new ChartControlSettings();
    settings.Name = "chart";
    settings.BorderOptions.Visibility = DefaultBoolean.False;
    settings.Height = 500;
    settings.Width = 600;
    settings.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside;

    Series series = new Series("Counties", options.View);
    series.LabelsVisibility = options.ShowLabels ? DefaultBoolean.True : DefaultBoolean.False;
    ((PieSeriesLabel)series.Label).Position = options.LabelPosition;
    if (options.ValueAsPercent) {
        series.Label.TextPattern = "{A}: {VP:P0}";
    }
    else {
        series.Label.TextPattern = "{A}: {V:G}";
    }
    series.ArgumentDataMember = "Barrio";
    series.ValueDataMembers[0] = "Cantidad";
    settings.Series.Add(series);
    settings.BoundDataChanged = (s, e) => {
        MVCxChartControl chart = (MVCxChartControl)s;
        PieSeriesViewBase view = (PieSeriesViewBase)chart.Series[0].View;
        if (options.ExplodePoint != null) {
            SeriesPoint explodedPoint = null;
            foreach (SeriesPoint point in chart.Series[0].Points) {
                if (point.Argument == options.ExplodePoint) {
                    explodedPoint = point;
                    break;
                }
            }
            if (explodedPoint != null) {
                view.ExplodedPoints.Add(explodedPoint);
            }
        } else {
            view.ExplodeMode = options.ExplodeMode;
        }
    };
    SimpleDiagram3D diagram = settings.Diagram as SimpleDiagram3D;
    if (diagram != null) {
        diagram.RotationType = RotationType.UseAngles;
        diagram.RotationOrder = RotationOrder.ZXY;
        diagram.RotationAngleX = -35;
        diagram.RotationAngleZ = 15;
    } else {
        series.Label.TextColor = Color.Black;
        series.Label.BackColor = Color.Transparent;
        series.Label.Font = new Font("Tahoma", 8, FontStyle.Bold);
        series.Label.LineVisibility = DefaultBoolean.True;
        series.Label.Border.Visibility = DefaultBoolean.False;
    }
    series.Label.Antialiasing = options.LabelPosition == PieSeriesLabelPosition.Radial;

    settings.Titles.Add(new ChartTitle() {
        Font = new Font("Tahoma", 18),
        Text = "Cantidad de Pedidos Por Barrios"
    });
    settings.Titles.Add(new ChartTitle() {
        Alignment = StringAlignment.Far,
        Dock = ChartTitleDockStyle.Bottom,
        Font = new Font("Tahoma", 8),
        TextColor = Color.Gray,
        Text = "Trabajo de Campo y Diploma"
    });

    @Html.DevExpress().Chart(settings).Bind(Model).GetHtml()
}


